package com.qersoft.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qersoft.dao.WQbank;
import com.qersoft.entity.WQBankEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("WQInfoService")
public class WQInfoImpl extends ServiceImpl<WQbank, WQBankEntity>
        implements WQInfoService {
    @Autowired
    private WQbank wqbank;

    public WQInfoImpl(WQbank wqbank) {
        this.wqbank = wqbank;
    }


    @Override
    public String add(Long userId, Long questionId) {
        QueryWrapper<WQBankEntity> wqBankEntityQueryWrapper = new QueryWrapper();
        wqBankEntityQueryWrapper.eq("userId", userId);
        WQBankEntity wqBankEntity = wqbank.selectOne(wqBankEntityQueryWrapper);
        if (wqBankEntity == null) {
            return "error";
        } else {
            if(wqBankEntity.getArrayChange() == null){
                wqBankEntity.setArrayChange(questionId+",");
                wqbank.updateById(wqBankEntity);
                return "success";
            }
            else{
                String result= wqBankEntity.getArrayChange();
                result+=questionId+",";
                wqBankEntity.setArrayChange(result);
                wqbank.updateById(wqBankEntity);
                return "success";
            }

        }
    }

    @Override
    public String del(Long userId, Long questionId) {
        QueryWrapper<WQBankEntity> wqBankEntityQueryWrapper = new QueryWrapper();
        wqBankEntityQueryWrapper.eq("userId", userId);
        WQBankEntity wqBankEntity = wqbank.selectOne(wqBankEntityQueryWrapper);
        if (wqBankEntity == null) {
            return "error";
        } else {
            if(wqBankEntity.getArrayChange() == null){
                return "error";
            }
            else{
                String result= wqBankEntity.getArrayChange();
                result=result.replace(questionId+",","");
                wqBankEntity.setArrayChange(result);
                wqbank.updateById(wqBankEntity);
                return "success";
            }

        }
    }
}
